﻿using System;

namespace DO.DreamOnTrader.Quote
{
	/// <summary>
	/// 讀取kbar檔案，產生kbar事件
	/// </summary>
	public class KBarFileKBarBuilder : KBarFileLoader, IKBarBuilder
	{
		/// <summary>
		/// 建構式
		/// </summary>
		/// <param name="_fileFolder">檔案目錄</param>
		public KBarFileKBarBuilder(string _fileFolder) : base(_fileFolder) { }

		/// <summary>
		/// 建構式
		/// </summary>
		/// <param name="_fileFolder">檔案目錄</param>
		/// <param name="_parseStr">剖析文字</param>
		protected KBarFileKBarBuilder(string _fileFolder, string _parseStr) : base(_fileFolder, _parseStr) { }

		/// <summary>
		/// 存取kbar的交割時間來判斷是否換月
		/// </summary>
		private DateTime lastTradeTime;

		protected override void proecessTick(KBar kbar)
		{
			if (BuildingKBar != null)
				BuildingKBar(kbar);

			if ( lastTradeTime == null){
				//取得最後交易時間
				lastTradeTime = Tick.getCloseDate(kbar.Ktime);
			}

			if (kbar.Ktime > lastTradeTime)
			{
				if (BuildingNewKBar != null)
					BuildingNewKBar(kbar);
				lastTradeTime = Tick.getCloseDate(kbar.Ktime);
			}
		}

		protected override void toEndQuote()
		{
			if (EndQuote != null)
				EndQuote(null, EventArgs.Empty);
		}


		#region KBarBuilder 成員

		public event BuildingKBarEventHandler BuildingKBar;

		public event BuildingNewKBarEventHandler BuildingNewKBar;

		#endregion


		#region QuoteBuilder 成員

		public event EndQuoteEventHandler EndQuote;

		#endregion
	}
}
